package com.hxkj.controller;

import com.hxkj.config.SftpConfig;
import com.hxkj.domain.po.Templet;
import com.hxkj.domain.po.TemplteTree;
import com.hxkj.service.TemplteService;
import com.integral.mall.file.storage.sftp.SFTPStorage;
import com.platform.common.domain.result.ResultMessageStruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.platform.common.domain.result.ResultDataStruct;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.Callable;
/**
 * 模板控制层
 * <p>
 * Created by yaosy on 2018年6月11日
 */
@RestController
@RequestMapping("/templet")
public class TemplteController {

    private static final Logger LOGGER = LoggerFactory.getLogger(TemplteController.class);

    @Autowired
    private TemplteService templteService;

    @Resource
    private SftpConfig sftpConfig;

    /**
     * 新增模板
     *
     * @param templet
     * @throws Exception
     */
    @RequestMapping(value = "/saveContent", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<ResultDataStruct<Templet>> saveContent(@RequestBody Templet templet) {
        return ()-> templteService.saveContent(templet);
    }

    /**
     * 新增目录
     *
     * @param templet
     * @throws Exception
     */
    @RequestMapping(value = "/saveFolder", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<ResultDataStruct<Templet>> saveFolder(@RequestBody Templet templet) throws Exception {
        SFTPStorage sftpStorage =  sftpConfig.sftpStorage(sftpConfig.storageFactory());
        return ()-> templteService.saveFolder(templet, sftpStorage);
    }

    /**
     * 获取模板功能树
     *
     * @param templet
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getTempletTree",consumes  ="application/json;charset=UTF-8",method = RequestMethod.POST)
    public  Callable<ResultDataStruct<List<TemplteTree>>>  getTempletTree(@RequestBody Templet templet) {
        return ()-> templteService.getTempletTree(templet);
    }


    /**
     * 删除模板
     * @param templet
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/remove",consumes  ="application/json;charset=UTF-8",method = RequestMethod.POST)
    public  Callable<ResultMessageStruct>  remove(@RequestBody Templet templet) {
        return ()-> templteService.remove(templet);
    }

    /**
     * 获取单条模板数据
     *
     * @param templet
     * @throws Exception
     */
    @RequestMapping(value = "/get",consumes  ="application/json;charset=UTF-8",method = RequestMethod.POST)
    public  Callable<ResultDataStruct<Templet>> get(@RequestBody Templet templet) {
        return ()-> templteService.get(templet);
    }

    /**
     * 删除模板
     * @param templet
     * @return
     * @throws Exception
     */
    SFTPStorage sftpStorage = null;
    @RequestMapping(value = "/modify",consumes  ="application/json;charset=UTF-8",method = RequestMethod.POST)
    public  Callable<ResultMessageStruct>  modify(@RequestBody Templet templet)  throws Exception{
        if(templet.getIsDirectory() != 1)
            sftpStorage = sftpConfig.sftpStorage(sftpConfig.storageFactory());
        return ()-> templteService.modify(templet,sftpStorage);
    }


    /**
     * 模板列表下拉框
     *
     * @param templet
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getTree",consumes  ="application/json;charset=UTF-8",method = RequestMethod.POST)
    public  Callable<ResultDataStruct<List<TemplteTree>>> getTree(@RequestBody Templet templet) {
        return ()-> templteService.getTree(templet);
    }

    /**
     * 模板上传后需要保存的信息
     *
     * @param templet
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/saveUploadContent",consumes  ="application/json;charset=UTF-8",method = RequestMethod.POST)
    public  Callable<ResultDataStruct<Templet>> saveUploadContent(@RequestBody Templet templet) {
        return ()-> templteService.saveUploadContent(templet);
    }


    /**
     * 模板上传后需要保存的信息
     *
     * @param templet
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getAllTempletByTempletId",consumes  ="application/json;charset=UTF-8",method = RequestMethod.POST)
    public  Callable<ResultMessageStruct> getAllTempletByTempletId(@RequestBody Templet templet) {
        return ()-> templteService.getAllTempletByTempletId(templet);
    }


    /**
     * 获取单个模板
     *
     * @param templet
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getTemplet",consumes  ="application/json;charset=UTF-8",method = RequestMethod.POST)
    public  Callable<Templet> getTemplet(@RequestBody Templet templet) {
        return ()-> templteService.getTemplet(templet);
    }

    /**
     * @author Mr.zheng
     * @Description getTempletBySiteId 根据站点获取模板文件
     * @Date 10:01 2018/8/17
     * @Param [templet]
     * @return com.platform.common.domain.result.ResultDataStruct<java.util.List<com.hxkj.domain.po.Templet>>
     **/
    @RequestMapping(value = "/getTempletBySiteId", method = RequestMethod.POST,consumes = "application/json;charset=UTF-8")
    public Callable<ResultDataStruct<List<Templet>>> getTempletBySiteId(@RequestBody Templet templet) throws Exception{
        return ()-> templteService.getTempletBySiteId(templet);
    }


    /**
     * @author Mr.zheng
     * @Description modifyTempletMd5 修改添加模板文件的MD5值
     * @Date 10:17 2018/8/20
     * @Param [templet]
     * @return com.platform.common.domain.result.ResultMessageStruct
     **/
    @RequestMapping(value = "/modifyTempletMd5", method = RequestMethod.POST,consumes = "application/json;charset=UTF-8")
    public Callable<ResultMessageStruct> modifyTempletMd5(@RequestBody Templet templet) throws Exception{
        return ()-> templteService.modifyTempletMd5(templet);
    }

    /**
     * 可视化模板接口
     *
     * @param templet
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/visuaTempletTree", method = RequestMethod.POST,consumes = "application/json;charset=UTF-8")
    public Callable<ResultDataStruct<List<TemplteTree>>> visuaTempletTree(@RequestBody Templet templet) throws Exception{
        return ()-> templteService.visuaTempletTree(templet);
    }
}
